package com.thuanpq.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.thuanpq.R;

public class PhoneMasterActivity extends Activity {

	private static final int BLACK_LIST = 0;
	private static final int APP_CONFIG = 1;
	private static final int APP_EXIT = 2;

	private Context mContext;
	private ListView mListView;
	private String[] mItemList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		// executeReceiver();

		initView();

		initChild();

		bindListView();
	}

	private void initView() {
		mListView = (ListView) findViewById(R.id.main_list);
	}

	private void initChild() {
		mContext = getApplicationContext();
		mItemList = getResources().getStringArray(R.array.main_gridview);
	}

	private void bindListView() {

		mListView.setAdapter(new PhoneMasterActivityAdapter());
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent;
				switch (position) {
				case BLACK_LIST:
					intent = new Intent(mContext, BlackListActivity.class);
					startActivity(intent);
					break;
				case APP_CONFIG:
					intent = new Intent(mContext, PrefActivity.class);
					startActivity(intent);
					break;
				case APP_EXIT:
					finish();
					break;
				}
			}
		});
	}

	class PhoneMasterActivityAdapter extends BaseAdapter {

		LayoutInflater inflater;

		public PhoneMasterActivityAdapter() {
			inflater = LayoutInflater.from(mContext);
		}

		@Override
		public int getCount() {
			if (mItemList != null) {
				return mItemList.length;
			}
			return 0;
		}

		@Override
		public Object getItem(int position) {
			return mItemList[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.main_item, null);

				holder = new ViewHolder();
				holder.image = (ImageView) convertView.findViewById(R.id.image);
				holder.text = (TextView) convertView.findViewById(R.id.text);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.text.setText(mItemList[position]);

			switch (position) {
			case BLACK_LIST:
				holder.image.setImageResource(R.drawable.blacklist_icon);
				break;
			case APP_CONFIG:
				holder.image.setImageResource(R.drawable.app_config_icon);
				break;
			case APP_EXIT:
				holder.image.setImageResource(R.drawable.app_exit);
				break;
			}

			return convertView;
		}

		class ViewHolder {
			ImageView image;
			TextView text;
		}
	}
}
